/*
 * pgp-mpi.c
 *	  OpenPGP MPI 辅助函数。
 *
 * Copyright (c) 2005 Marko Kreen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * contrib/pgcrypto/pgp-mpi.c
 */
#include "postgres.h"

#include "pgp.h"
#include "px.h"

int pgp_mpi_alloc(int fc_bits, PGP_MPI **fc_mpi)
{
	PGP_MPI    *fc_n;
	int			fc_len = (fc_bits + 7) / 8;

	if (fc_bits < 0 || fc_bits > 0xFFFF)
	{
		px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", fc_bits);
		return PXE_PGP_CORRUPT_DATA;
	}
	fc_n = palloc(sizeof(*fc_n) + fc_len);
	fc_n->bits = fc_bits;
	fc_n->bytes = fc_len;
	fc_n->data = (uint8 *) (fc_n) + sizeof(*fc_n);
	*fc_mpi = fc_n;
	return 0;
}

int pgp_mpi_create(uint8 *fc_data, int fc_bits, PGP_MPI **fc_mpi)
{
	int			fc_res;
	PGP_MPI    *fc_n;

	fc_res = pgp_mpi_alloc(fc_bits, &fc_n);
	if (fc_res < 0)
		return fc_res;
	memcpy(fc_n->data, fc_data, fc_n->bytes);
	*fc_mpi = fc_n;
	return 0;
}

int pgp_mpi_free(PGP_MPI *fc_mpi)
{
	if (fc_mpi == NULL)
		return 0;
	px_memset(fc_mpi, 0, sizeof(*fc_mpi) + fc_mpi->bytes);
	pfree(fc_mpi);
	return 0;
}

int pgp_mpi_read(PullFilter *fc_src, PGP_MPI **fc_mpi)
{
	int			fc_res;
	uint8		fc_hdr[2];
	int			fc_bits;
	PGP_MPI    *fc_n;

	fc_res = pullf_read_fixed(fc_src, 2, fc_hdr);
	if (fc_res < 0)
		return fc_res;
	fc_bits = ((unsigned) fc_hdr[0] << 8) + fc_hdr[1];

	fc_res = pgp_mpi_alloc(fc_bits, &fc_n);
	if (fc_res < 0)
		return fc_res;

	fc_res = pullf_read_fixed(fc_src, fc_n->bytes, fc_n->data);
	if (fc_res < 0)
		pgp_mpi_free(fc_n);
	else
		*fc_mpi = fc_n;
	return fc_res;
}

int pgp_mpi_write(PushFilter *fc_dst, PGP_MPI *fc_n)
{
	int			fc_res;
	uint8		fc_buf[2];

	fc_buf[0] = fc_n->bits >> 8;
	fc_buf[1] = fc_n->bits & 0xFF;
	fc_res = pushf_write(fc_dst, fc_buf, 2);
	if (fc_res >= 0)
		fc_res = pushf_write(fc_dst, fc_n->data, fc_n->bytes);
	return fc_res;
}

int pgp_mpi_hash(PX_MD *fc_md, PGP_MPI *fc_n)
{
	uint8		fc_buf[2];

	fc_buf[0] = fc_n->bits >> 8;
	fc_buf[1] = fc_n->bits & 0xFF;
	px_md_update(fc_md, fc_buf, 2);
	px_md_update(fc_md, fc_n->data, fc_n->bytes);

	return 0;
}

unsigned pgp_mpi_cksum(unsigned fc_cksum, PGP_MPI *fc_n)
{
	int			fc_i;

	fc_cksum += fc_n->bits >> 8;
	fc_cksum += fc_n->bits & 0xFF;
	for (fc_i = 0; fc_i < fc_n->bytes; fc_i++)
		fc_cksum += fc_n->data[fc_i];

	return fc_cksum & 0xFFFF;
}
